package richbar.com.richbar.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.common.base.BaseActivity;
import com.common.help.RecycleViewHelp;
import com.common.util.LogUtil;
import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import richbar.com.richbar.R;
import richbar.com.richbar.base.BaseListAdapter;
import richbar.com.richbar.bean.CateRightResult;
import richbar.com.richbar.bean.DeleteRateResult;
import richbar.com.richbar.bean.MyExchangeBean;
import richbar.com.richbar.bean.RateResult;
import richbar.com.richbar.help.TitleHelp;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.view.NoScrollListview;

/**
 * Created by Administrator on 2018/11/30.
 * 我的评价
 */

public class MyEvaluate extends BaseActivity {
    @BindView(R.id.swipe)
    SwipeRefreshLayout swipe;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    private NewsAdapter adapter;
    private LoadingDialog loadingDialog;

    @Override
    public int initLayout() {
        return R.layout.ac_my_evaluate;
    }

    @Override
    public void initTitle() {
        new TitleHelp(this).setTitle("我的评价");
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this,0);
        swipe.setColorSchemeResources(R.color.blue);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(false);
            }
        });
        swipe.setRefreshing(true);
        adapter = new NewsAdapter(R.layout.adapter_myevaluate_item, new ArrayList<RateResult.DataEntity.ListEntity>());
        RecycleViewHelp.setManager(recycleView, this);
        recycleView.setAdapter(adapter);
        adapter.openLoadMore(1,true);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                LogUtil.e("IIIIIIIIIIII","onLoadMoreRequested" + page);
                getData(true);
            }
        });
    }

    @Override
    public void initData() {
        getData(false);
    }

    private void getData(final boolean loadMore) {
        if (loadMore){
            pageAdd();
        }else {
            pageReset();
        }
        NetWorkUtil.myRate(page + "", "4", new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final RateResult result = MyGson.getInstance().fromJson(data, RateResult.class);
                if (checkIsOk(result)){
                    setData(result,loadMore);
                    refreshDismiss(swipe);
                }
            }
        });
    }

    private void setData(RateResult result,boolean loadMore) {
        LogUtil.e("DDDDDDDDDDD",0 + "----" + page);

        if (page > result.getData().getTotalpage()){
            adapter.isNextLoad(false);
            LogUtil.e("DDDDDDDDDDD",1);
            return;
        }
        List<RateResult.DataEntity.ListEntity> allData = adapter.getData();
        if (loadMore) {
            LogUtil.e("DDDDDDDDDDD",4);
            allData.addAll(result.getData().getList());
            adapter.isNextLoad(true);
        } else {
            LogUtil.e("DDDDDDDDDDD",5);
            allData = result.getData().getList();
        }
        adapter.setNewData(allData);
    }

    private class NewsAdapter extends BaseQuickAdapter<RateResult.DataEntity.ListEntity> {
        public NewsAdapter(int layoutResId, List<RateResult.DataEntity.ListEntity> data) {
            super(layoutResId, data);

        }

        @Override
        protected void convert(BaseViewHolder holder, final RateResult.DataEntity.ListEntity bean) {
            //holder.setText(R.id.shop_name,bean.get)
            NoScrollListview listview = holder.getView(R.id.listView);
            ImageView iv = holder.getView(R.id.pic);
            GlideUtil.loadImage(mContext,NetWorkUtil.URL + bean.getImg(),iv);
            holder.setText(R.id.goods_name,bean.getName());
            holder.setText(R.id.num,"共" + bean.getNum() +"件商品");
            holder.setText(R.id.price,"需付款:" + bean.getPrice() +"元");

            View line = holder.getView(R.id.line);
            Button comment = holder.getView(R.id.comment);
            if ( bean.getComment() == 0){
                line.setVisibility(View.VISIBLE);
                comment.setVisibility(View.VISIBLE);
                holder.setOnClickListener(R.id.comment,new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startForResult(EvaluateDetailAc.class, 501,new OnBundleListener() {
                            @Override
                            public Bundle onSetBundle(Bundle bundle) {
                                bundle.putString("goods_id",bean.getId()+"");
                                bundle.putString("goods_img",bean.getImg());
                                bundle.putString("oid",bean.getOid()+"");
                                return bundle;
                            }
                        });
                    }
                });
            }else {
                line.setVisibility(View.GONE);
                comment.setVisibility(View.GONE);
            }
            holder.setOnClickListener(R.id.del, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    delRate(bean.getId() + "");
                }
            });
        }

        //class MyAdapter extends BaseListAdapter<>
    }

    private void delRate(String id) {
        loadingDialog.show();
        NetWorkUtil.delRate(id, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final DeleteRateResult result = MyGson.getInstance().fromJson(data, DeleteRateResult.class);
                if (checkIsOk(result)){
                    getData(false);
                    loadingDialog.dismiss();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 501 && resultCode == RESULT_OK){
            getData(false);
        }
    }
}
